<template>
  <view v-if="visible" class="app-modal" :class="modalClass" @click="handleMaskClick">
    <!-- 遮罩层 -->
    <view class="app-modal__mask" :style="maskStyle"></view>

    <!-- 弹窗内容 -->
    <view class="app-modal__content" :style="contentStyle" @click.stop>
      <!-- 关闭按钮 -->
      <view v-if="showClose" class="app-modal__close" @click="handleClose">
        <text class="app-modal__close-icon">×</text>
      </view>

      <!-- 标题 -->
      <view v-if="title" class="app-modal__header">
        <text class="app-modal__title">{{ title }}</text>
      </view>

      <!-- 内容区域 -->
      <view class="app-modal__body">
        <slot>
          <text class="app-modal__text">{{ content }}</text>
        </slot>
      </view>

      <!-- 操作按钮 -->
      <view v-if="showActions" class="app-modal__footer">
        <button
          v-if="showCancel"
          class="app-modal__btn app-modal__btn--cancel"
          @click="handleCancel"
        >
          {{ cancelText }}
        </button>
        <button
          v-if="showConfirm"
          class="app-modal__btn app-modal__btn--confirm"
          @click="handleConfirm"
        >
          {{ confirmText }}
        </button>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, computed, watch } from 'vue'

// Props
interface Props {
  visible?: boolean
  title?: string
  content?: string
  type?: 'alert' | 'confirm' | 'prompt' | 'custom'
  showClose?: boolean
  maskClosable?: boolean
  showCancel?: boolean
  showConfirm?: boolean
  cancelText?: string
  confirmText?: string
  width?: string
  zIndex?: number
  customClass?: string
}

const props = withDefaults(defineProps<Props>(), {
  visible: false,
  title: '',
  content: '',
  type: 'alert',
  showClose: false,
  maskClosable: true,
  showCancel: true,
  showConfirm: true,
  cancelText: '取消',
  confirmText: '确定',
  width: '80%',
  zIndex: 1000,
  customClass: ''
})

// Emits
const emit = defineEmits<{
  'update:visible': [visible: boolean]
  close: []
  cancel: []
  confirm: []
  maskClick: []
}>()

// 计算属性
const modalClass = computed(() => {
  return [
    `app-modal--${props.type}`,
    {
      'app-modal--visible': props.visible
    },
    props.customClass
  ]
})

const maskStyle = computed(() => {
  return {
    zIndex: props.zIndex
  }
})

const contentStyle = computed(() => {
  return {
    width: props.width,
    zIndex: props.zIndex + 1
  }
})

// 监听 visible 变化
watch(() => props.visible, (newVal) => {
  if (newVal) {
    // 弹窗显示时的处理
    uni.hideTabBar()
  } else {
    // 弹窗隐藏时的处理
    uni.showTabBar()
  }
})

// 方法
/**
 * 处理遮罩点击
 */
const handleMaskClick = () => {
  if (props.maskClosable) {
    handleClose()
  }
  emit('maskClick')
}

/**
 * 处理关闭
 */
const handleClose = () => {
  emit('update:visible', false)
  emit('close')
}

/**
 * 处理取消
 */
const handleCancel = () => {
  emit('cancel')
  handleClose()
}

/**
 * 处理确认
 */
const handleConfirm = () => {
  emit('confirm')
  handleClose()
}
</script>

<style lang="scss" scoped>
.app-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  opacity: 0;
  visibility: hidden;
  transition: all 0.3s ease;

  &--visible {
    opacity: 1;
    visibility: visible;
  }

  &__mask {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(2px);
  }

  &__content {
    position: relative;
    background: #ffffff;
    border-radius: 12px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.15);
    max-height: 80vh;
    overflow: hidden;
    transform: scale(0.8) translateY(20px);
    transition: all 0.3s ease;
  }

  &__close {
    position: absolute;
    top: 16px;
    right: 16px;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    background: #f5f5f5;
    cursor: pointer;
    transition: all 0.3s ease;
    z-index: 1;

    &:active {
      background: #e8e8e8;
    }
  }

  &__close-icon {
    font-size: 20px;
    color: #666;
    line-height: 1;
  }

  &__header {
    padding: 24px 24px 0 24px;
    text-align: center;
  }

  &__title {
    font-size: 18px;
    font-weight: 600;
    color: #333;
    line-height: 1.4;
  }

  &__body {
    padding: 16px 24px 24px 24px;
    text-align: center;
  }

  &__text {
    font-size: 14px;
    color: #666;
    line-height: 1.6;
  }

  &__footer {
    display: flex;
    border-top: 1px solid #f0f0f0;
  }

  &__btn {
    flex: 1;
    height: 48px;
    border: none;
    background: transparent;
    font-size: 16px;
    cursor: pointer;
    transition: all 0.3s ease;
    border-radius: 0;

    &:first-child {
      border-bottom-left-radius: 12px;
    }

    &:last-child {
      border-bottom-right-radius: 12px;
    }

    &:active {
      background: #f5f5f5;
    }

    &--cancel {
      color: #666;
      border-right: 1px solid #f0f0f0;
    }

    &--confirm {
      color: #1890ff;
      font-weight: 500;
    }
  }

  // 不同类型的样式
  &--alert {
    .app-modal__btn--confirm {
      color: #1890ff;
    }
  }

  &--confirm {
    .app-modal__btn--confirm {
      color: #1890ff;
    }
  }

  &--visible {
    .app-modal__content {
      transform: scale(1) translateY(0);
    }
  }
}

// 内容动画
.app-modal--visible .app-modal__content {
  animation: modalFadeIn 0.3s ease forwards;
}

@keyframes modalFadeIn {
  from {
    opacity: 0;
    transform: scale(0.8) translateY(20px);
  }
  to {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}
</style>